package com.jiuyin.dianjing.ui.fragment.match;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.event.RefreshEvent;
import com.jiuyin.dianjing.model.MatchApplyListModel;
import com.jiuyin.dianjing.ui.activity.match.MatchApplyInfoActivity;
import com.jiuyin.dianjing.ui.fragment.base.BaseRefreshFragment;
import com.jiuyin.dianjing.util.DisplayUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMPETITIONID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_PAGE_NUM;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_FIRST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_NUM;

/**
 * Description:
 * File: FragmentMatchApplyList.java
 * Author: Administrator
 * Create: 2020/5/20 16:13
 */
public class FragmentMatchApplyList extends BaseRefreshFragment<MatchApplyListModel> {
    private String competitionId;
    public FragmentMatchApplyList() {
        super(PAGE_NUM);
    }

    public static FragmentMatchApplyList newInstance(String competitionId) {
        FragmentMatchApplyList fragment = new FragmentMatchApplyList();
        Bundle args = new Bundle();
        args.putString(KEY_COMPETITIONID, competitionId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void fetchData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, HelperApplication.getToken());
        map.put(KEY_COMPETITIONID, competitionId);
        map.put(KEY_PAGE_NUM, mPageNum);
        JSONObject data = new JSONObject(map);
        ApiResultCallback request = new DefaultApiResult() {

            @Override
            public void onSuccess(JsonObject data) {
                if (null != data && data.has("applyForList")) {
                    List<MatchApplyListModel> matchList = new Gson().fromJson(data.getAsJsonArray("applyForList"), new TypeToken<List<MatchApplyListModel>>() {
                    }.getType());
                    dealWithSuccessInfo(matchList);
                } else {
                    dealOtherInfo();
                }
            }

            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                dealOtherInfo();
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dealOtherInfo();
            }
        };
        ServerApi.post(ApiEnum.GET_APPLY_FOR_FIRST_ALL, data, null, progress, null, request);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.layout_fresh_with_recycle_view_empty;
    }

    @Override
    protected void initData() {
        if (getArguments() != null) {
            competitionId = getArguments().getString(KEY_COMPETITIONID);
        }
    }

    @Override
    protected void initView() {
        mAdapter = new BaseRecyclerAdapter<MatchApplyListModel>(R.layout.item_team_apply_list) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, MatchApplyListModel model, int position) {
                ImageView ivHead = holder.itemView.findViewById(R.id.iv_head);
                Glide.with(getContext())
                        .load(model.header)
                        .override(DisplayUtil.dp2px(getContext(), 26.7f))
                        .into(ivHead);
                holder.text(R.id.tv_name, model.nickname);
                holder.itemView.setOnClickListener(v -> {
                    Intent intent = new Intent(getContext(), MatchApplyInfoActivity.class);
                    intent.putExtra("info", model);
                    startActivity(intent);
                });
            }
        };
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        mRcvPage.addItemDecoration(divider);
        mRcvPage.setAdapter(mAdapter);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RefreshEvent event) {
        if (event.clazzName.equals(getClass().getSimpleName())) {
            mPageNum = PAGE_FIRST;
            mAdapter.clear();
            fetchData();
        }
    }
}
